<?php

class Adept_Db_SqlBatchParser
{

    static function parse($sql, $delimiter = ';') 
    {
        $result = array();
        $sql = preg_replace('~^((?:\/\/)|(?:\#).*)$~m', '', $sql);
        $in_string = false;
        $quote = '';

        $quotes = array('`', '"', "'");
        $current = '';

        for ($i = 0; $i < strlen($sql); $i++)
        {
            if(substr($sql, $i, strlen($delimiter)) == $delimiter && !$in_string)
            {
                $current = trim($current);
                if (!empty($current))
                $result[] = $current;
                $current = '';
            }
            else
            {
                $current .= $sql[$i];
                if (in_array($sql[$i], $quotes))
                {
                    if ($in_string)
                    $in_string = ($sql[$i] == $quote) ? false : true;
                    else    {
                        $in_string = true;
                        $quote = $sql[$i];
                    }
                }
            }
        }
        return $result;
    }

}